package app.fugema.shop.main.setting.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.BaseConstant;
import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.lljjcoder.style.citylist.Toast.ToastUtils;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityCenterModefyPwdBinding;
import app.fugema.shop.main.login.view.ForgetPassWordActivity;
import app.fugema.shop.main.setting.vm.SettingUserVM;
import app.fugema.shop.util.SimpleTextWatcher;

public class ModefyLoginPwdActivity extends BaseTitleActivity<ActivityCenterModefyPwdBinding, SettingUserVM> {

    public static void start(Context context , Boolean isPayPwd)
    {
        Intent intent = new Intent(context, ModefyLoginPwdActivity.class);
        intent.putExtra("isPayPwd",isPayPwd);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_center_modefy_pwd);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    private String userKey;

    private Boolean isPayPwd = false;

    @Override
    protected void initView()
    {
        super.initView();

        setCloseImg(R.mipmap.ic_back_white);
        setTitle("修改登录密码",R.color.white);


        binding.etPassWordRepeat.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(String content)
            {
                String oldStr = binding.etOldPassWord.getText().toString();
                String newPwdStr = binding.etPassWordNew.getText().toString();

                if(UIUtils.isNotEmpty( content ) && UIUtils.isNotEmpty(oldStr) && UIUtils.isNotEmpty( newPwdStr ) )
                {
                    binding.btnSubmit.setEnabled(true);
                    binding.btnSubmit.setAlpha(1f);
                }
                else
                {
                    binding.btnSubmit.setEnabled(false);
                    binding.btnSubmit.setAlpha(0.6f);
                }
            }
        });

        binding.btnFindPayPwd.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                //ToastUtils.showShortToast(mContext ,"忘记支付密码");
                ForgetPassWordActivity.lunch(mContext, "找回支付密码");
            }
        });

        //默认不让 提交
        binding.btnSubmit.setEnabled(false);
        binding.btnSubmit.setAlpha(0.6f);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);

        isPayPwd = intent.getBooleanExtra("isPayPwd",false);

        showModefyPayPwd(isPayPwd);

        userKey = LoginUtils.getToken();

        binding.btnSubmit.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                if( isPayPwd )
                {
                    postChangePayPwd();
                }
                else
                {
                    postChangePwd();
                }
            }
        });

    }

    /**
     *  提交密码
     */
    private void postChangePwd()
    {
         String oldPwd = binding.etOldPassWord.getText().toString().trim();
         String newPwd1 = binding.etPassWordNew.getText().toString().trim();
         String newPwd2 = binding.etPassWordRepeat.getText().toString().trim();

         if( UIUtils.isNotEmpty( newPwd1 ) && UIUtils.isNotEmpty( newPwd2 ) && newPwd1.equals( newPwd2 ) )
         {
             viewModel.postChangePwd( oldPwd,newPwd1,userKey )
                      .observe(this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                      {
                          @Override
                          protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                          {
                                 if( data.data.isSuccess() )
                                 {
                                       UIUtils.showToastSafes( data.data.getMsg() );
                                       LiveEventBus.get(BaseConstant.LOGIN_IN).post(1);
                                       finish();
                                 }
                          }
                      });
         }
         else
         {
            UIUtils.showToastSafes("新密码两次输入不一致");
         }
    }

    /**
     *  提交修改支付密码
     */
    private void postChangePayPwd()
    {
        String oldPwd = binding.etOldPassWord.getText().toString().trim();
        String newPwd1 = binding.etPassWordNew.getText().toString().trim();
        String newPwd2 = binding.etPassWordRepeat.getText().toString().trim();

        if( UIUtils.isNotEmpty( newPwd1 ) && UIUtils.isNotEmpty( newPwd2 ) && newPwd1.equals( newPwd2 ) )
        {
            viewModel.postChangePayPwd( oldPwd,newPwd1,userKey )
                    .observe(this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                    {
                        @Override
                        protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                        {
                            if( data.data.isSuccess() )
                            {
                                UIUtils.showToastSafes( data.data.getMsg() );
                                finish();
                            }
                        }
                    });
        }
        else
        {
            UIUtils.showToastSafes("新密码两次输入不一致");
        }
    }


    private void showModefyPayPwd(Boolean isShowPay)
    {
        if( isShowPay )
        {
            setTitle("修改支付密码",R.color.white);
            binding.btnFindPayPwd.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
